package com.yneusoft.common.tool;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;

public class JWTUtils {
    private static final String TOKEN="FanWanXing";
    //生成token
    public static String getToken(Integer aid){
        JWTCreator.Builder builder= JWT.create();
        builder.withClaim("aid",String.valueOf(aid));
        Calendar ins=Calendar.getInstance();
        ins.add(Calendar.HOUR,72);
        builder.withExpiresAt(ins.getTime());
        return builder.sign(Algorithm.HMAC256(TOKEN));
    }
    //验证token
    public static void verify(String token){
        JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
    }

    //获取token中的payload
    public static DecodedJWT getToken(String token){
        return JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
    }


    public static String getClaim(String token,String key){
        DecodedJWT decodedJWT=JWT.decode(token);
        return decodedJWT.getClaim(key).asString();
    }
}
